package com.evildog.workspace.dependencies.mybatis.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.evildog.workspace.security.jwt.util.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * 更新是填充
 */
@Slf4j
public class UpdateMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "createUserId", Long.class, SecurityUtil.getUserId());
        this.strictInsertFill(metaObject, "userId", Long.class, SecurityUtil.getUserId());

        updateFill(metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictUpdateFill(metaObject, "updateUserId", Long.class, SecurityUtil.getUserId());
    }
}